﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;

namespace GPM
{
    public partial class FrmResourceAuth : BaseForm
    {
        GridCheckMarksSelection authSelection;
        GridCheckMarksSelection unAuthSelection;
        private int SelectedWebpartID;
        private int SelectedResourceID;
        private int SelectedMenuID;
        public FrmResourceAuth()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void btnResourceQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(editResourceName.Text))
            {
                ExMessageBox.Show("资源名不能为空", AppSetter.SoftName);
                return;
            }
            try
            {
                UserFuncController uc = new UserFuncController();
                DataTable dt = uc.GetResourceInfo(this.radioType.Properties.Items[
                    this.radioType.SelectedIndex].Value.ToString(),
                    this.editResourceName.Text);

                gcResource.DataSource = dt;
            }
            catch (Exception ex)
            {
                ExMessageBox.Show("提示信息：错误信息[" + ex.Message + "]", AppSetter.SoftName);
                this.ActiveControl = this.editResourceName;
            }
        }

        private void FrmResourceAuth_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.editResourceName;
            authSelection = new GridCheckMarksSelection(gvAuthGroup);
            unAuthSelection = new GridCheckMarksSelection(gvUnAuthGroup);
        }

        private void editResourceName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnResourceQuery.PerformClick();
            }
        }

        private void gvResource_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gvResource.GetFocusedDataRow();
            if (null == dr)
                return;
            int webpartid = Convert.ToInt32(dr["webpart_id"] ?? "0");
            int resourceid = Convert.ToInt32(dr["resource_id"] ?? "0");
            int menuid = Convert.ToInt32(dr["menu_id"]??"0");
            this.SelectedWebpartID = webpartid;
            this.SelectedResourceID = resourceid;
            this.SelectedMenuID = menuid;
            BindingUserGroup(menuid, resourceid,webpartid);
        }

        private void BindingUserGroup(int menuid, int resourceid, int webpartid)
        {
            if (0 == menuid)
            {
                UserGroupController gc = new UserGroupController();
                gcAuthGroup.DataSource = gc.GetGroupByMenuID(resourceid);
                gcUnAuthGroup.DataSource = gc.GetUnAuthGroupByMenuID(resourceid);
            }
            else
            {
                UserGroupController gc = new UserGroupController();
                gcAuthGroup.DataSource = gc.GetGroupByButtonID(webpartid, resourceid);
                gcUnAuthGroup.DataSource = gc.GetUnAuthGroupByButtonID(webpartid, resourceid);
            }
        }

        private bool CheckInput()
        {
            if (this.SelectedResourceID == 0)
            {
                ExMessageBox.Show("提示信息：没有选中资源数据", AppSetter.SoftName);
                return false;
            }
            return true;
        }

        private bool CheckUnAuth()
        {
            if (unAuthSelection.SelectedCount == 0)
            {
                ExMessageBox.Show("提示信息：没有选中未授权角色", AppSetter.SoftName);
                return false;
            }
            return true;
        }

        private bool CheckAuth()
        {
            if (authSelection.SelectedCount == 0)
            {
                ExMessageBox.Show("提示信息：没有选中已授权角色", AppSetter.SoftName);
                return false;
            }
            return true;
        }

        private void btnAuth_Click(object sender, EventArgs e)
        {
            if ((CheckInput()) && (CheckUnAuth()))
            {
                IList<int> groupids = new List<int>();
                foreach (object o in unAuthSelection.Selection)
                {
                    User_Group group = o as User_Group;
                    if (null != group)
                        groupids.Add(group.group_id);
                }
                try
                {
                    ResourceAuthController ctler = new ResourceAuthController();
                    ctler.ResourceAuth(SelectedMenuID,SelectedWebpartID, SelectedResourceID, groupids);
                    LogHelper.Log("资源授权", "授权",  "SelectedWebpartID:" + SelectedWebpartID.ToString(), "SelectedResourceID:" + SelectedResourceID.ToString(), "groupids" + string.Join(",", groupids));
                    ExMessageBox.Show("提示信息：授权成功！", AppSetter.SoftName);
                    BindingUserGroup(SelectedMenuID, SelectedResourceID,SelectedWebpartID);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void btnCancelAuth_Click(object sender, EventArgs e)
        {
            if ((CheckInput()) && (CheckAuth()))
            {
                IList<int> groupids = new List<int>();
                foreach (object o in authSelection.Selection)
                {
                    User_Group group = o as User_Group;
                    if (null != group)
                        groupids.Add(group.group_id);
                }
                try
                {
                    ResourceAuthController ctler = new ResourceAuthController();
                    ctler.ResourceUnAuth(SelectedMenuID,SelectedWebpartID, SelectedResourceID, groupids);
                    LogHelper.Log("资源授权", "取消授权", "SelectedWebpartID:" + SelectedWebpartID.ToString(), "SelectedResourceID:" + SelectedResourceID.ToString(), "groupids" + string.Join(",", groupids));
                    ExMessageBox.Show("提示信息：取消授权成功！", AppSetter.SoftName);
                    BindingUserGroup(SelectedMenuID,SelectedWebpartID, SelectedResourceID);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }
    }
}